package com.cdd.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.goods.mapper.GoodsStorageMapper;
import com.cdd.goods.mapper.GoodsTypeMapper;
import com.cdd.goods.service.IGoodsStorageService;
import com.common.entity.GoodsBrand;
import com.common.entity.GoodsSpecification;
import com.common.entity.GoodsStorage;
import com.common.entity.GoodsType;
import com.common.untils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-01
 */
@Service
public class GoodsStorageServiceImpl implements IGoodsStorageService {
    @Autowired
    private GoodsStorageMapper goodsStorageMapper;
    @Autowired
    private GoodsTypeMapper goodsTypeMapper;
    @Override
    public CommonResult selectAll(Integer currentPage, Integer pageSize, GoodsStorage goodsStorage) {
        Page<GoodsStorage> page=new Page<>(currentPage,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(goodsStorage.getGdname())){
            wrapper.like("gdname",goodsStorage.getGdname());
        }
        if (goodsStorage.getIsDeleted()!=null){
            wrapper.eq("is_deleted",goodsStorage.getIsDeleted());
        }
        Page Page1 = goodsStorageMapper.selectPage(page, wrapper);
        return new CommonResult(2000,"查询成功",Page1);
    }

    @Override
    public CommonResult delectGoodsStorage(Integer id) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);

        GoodsStorage goodsStorage=new GoodsStorage();
        goodsStorage.setIsDeleted(1);
        goodsStorageMapper.update(goodsStorage,wrapper);
        return new CommonResult(2000, "删除成功", null);
    }

    @Override
    public CommonResult updateGoodsStorage(GoodsStorage goodsStorage) {

        GoodsStorage goodsStorage1=new GoodsStorage();
        goodsStorage1.setId(goodsStorage.getId());
        goodsStorage1.setGdname(goodsStorage.getGdname());
        goodsStorageMapper.updateById(goodsStorage1);
        return new CommonResult(2000, "修改成功", null);
    }

    @Override
    public CommonResult insertStorage(GoodsStorage goodsStorage) {

        goodsStorage.setIsDeleted(0);
        List<GoodsStorage> goodsBrands = goodsStorageMapper.selectGdname();
        for (GoodsStorage g:goodsBrands) {
            if (g.getGdname().equals(goodsStorage.getGdname())){
                return new CommonResult(5000, "添加失败，已有该存储方式", null);
            }

        }
        goodsStorageMapper.insert(goodsStorage);
        return new CommonResult(2000, "添加成功", null);
    }

    @Override
    public CommonResult select() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        List list = goodsStorageMapper.selectList(wrapper);
        return new CommonResult(2000, "查询成功", list);
    }

    @Override
    public List<GoodsType> selectGoodsType() {
        QueryWrapper<GoodsType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        List<GoodsType> list = goodsTypeMapper.selectList(queryWrapper);
        return list;
    }
}
